<template>
  <div style="display: none">
    <blood v-if="name === 'blood'" ref="ele" @fetchData="fetchData" />
    <match v-if="name === 'match'" ref="ele" @fetchData="fetchData" />
  </div>
</template>

<script>
  import { doPrints } from '@/api/blood'
  import Blood from './blood'
  import Match from './match'
  import { getLodop } from '@/utils/LodopFuncs'
  import { isArray } from 'lodash'
  import { mapGetters } from 'vuex'
  export default {
    components: {
      Blood,
      Match,
    },
    props: {
      name: {
        type: String,
        default() {
          return ''
        },
      },
    },
    data() {
      return {
        dialogVisible: false,
        title: '标签打印',
        bloods: [],
      }
    },
    computed: {
      ...mapGetters(['systemSet']),
    },
    methods: {
      show(row) {
        this.dialogVisible = true
        if (this.name === 'blood') {
          this.bloods = row
        }
        setTimeout(() => {
          this.$refs.ele.show(row)
        }, 50)
      },
      async doPrints() {
        const { msg } = await doPrints({
          ids: this.bloods.map((val) => val.id),
        })
        this.$emit('getAll')
        this.$baseMessage(msg, 'success', 'vab-hey-message-success')
      },
      close() {
        this.bloods = []
        this.$emit('close')
      },
      fetchData() {
        console.log('fetchData')
        this.$emit('fetch-data')
      },
      printByLodop(strHtml, bodyStyle, type, width, height) {
        var LODOP = getLodop() //调用getLodop获取LODOP对象
        LODOP.PRINT_INIT('')
        // LODOP.SET_ZOOM_MODE(LODOP.ZOOM_WIDTH, 100)
        // LODOP.SET_PRINT_SCALE(100)
        // LODOP.SET_PRINT_PAGESIZE(2)
        LODOP.SET_PRINT_PAGESIZE(type, width, height)
        LODOP.SET_PRINT_STYLEA(3, 'FontName', '微软雅黑')
        console.log(isArray(strHtml))
        if (isArray(strHtml)) {
          strHtml.forEach((el, index) => {
            if (el.trim()) {
              // 确保内容不为空
              el = `<style>${bodyStyle}</style><body>${el}</body>`
              LODOP.ADD_PRINT_HTM(0, 0, '100%', '100%', el)
              if (index < strHtml.length - 1) {
                LODOP.NewPage() // 只在非最后一页添加新页面
              }
            }
          })
        } else {
          if (strHtml.trim()) {
            strHtml = `<style>${bodyStyle}</style><body>${strHtml}</body>`
            LODOP.ADD_PRINT_HTM(0, 0, '100%', '100%', strHtml)
          }
        }
        if (this.systemSet.silent_preview) {
          LODOP.PREVIEW()
        } else {
          console.log('***********print***********')
          LODOP.PRINT()
        }
      },
    },
  }
</script>

<style lang="scss" scoped></style>
